<?php
namespace App\Http\Controllers\Admin;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use App\Services\Tools\JwtService;
use App\Services\Tools\ObsService;
use App\Services\Tools\CommonService;
use App\Http\Controllers\BaseController;

class LisenceController extends BaseController
{
    /**
     * @api {post} /lisence/type_list 资质分类列表
     * @apiVersion 0.1.0
     * @apiName 资质分类列表
     * @apiGroup  资质管理
     * @apiParam {number} page     页数(不传默认第一页)，非必传
     * @apiParam {number} perPage     每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list(数据列表)": [
     * {
     * "lisence_id（资质id）": 2,
     * "lisence_name（分类名称）": "游戏",
     * "icon（'图标地址）": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/test/lisence_icon/b0cdf12d33e73261368bfd5370aef106DFhAs.png",
     * "sort（排序）": 3,
     * "pid（父id）": 0,
     * "level（层级）": 1,
     * "add_time（添加时间）": "2020-08-28 15:42:02",
     * "status（状态：1正常2暂停3删除）": 1
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 2
     * }
     * }
     * }
     */
    public function type_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'page'    => 'filled|numeric',
                'perPage' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $list = app('repo_lisence')->typeList($page, $perPage);
        if ($list) {
            foreach ($list as $key => $value) {
                $value->add_time = date('Y-m-d H:i:s', $value->add_time);
            }
        }

        return Response::sendList($list);
    }

    /**
     * @api {post} /lisence/add_type 新增资质分类
     * @apiVersion 0.1.0
     * @apiName 新增资质分类
     * @apiGroup 资质管理
     * @apiParam {string} lisence_name   资质名称，必传
     * @apiParam {file} icon    图标地址 ，必传
     * @apiParam {number} sort    排序，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93115  渠道新增失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93115,
     *          "message": "errorMsg"
     *      }
     */
    public function add_type(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_name' => 'required',
                'icon'         => 'required',
                'sort'         => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $lisence_name = $request->input('lisence_name');
        $sort = $request->input('sort', 0);
        $icon = $request->file('icon');
        $res = ObsService::uploadFile($icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['icon'] = $res;
        }
        $param['add_time'] = time();
        $param['lisence_name'] = $lisence_name;
        $param['level'] = 1;
        $param['sort'] = $sort;
        $param['pause_icon'] = '';
        $param['exp_image'] = '';
        $param['prompt_image'] = '';
        $param['prompt_voice'] = '';
        app('repo_lisence')->addType($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/save_type 编辑资质分类
     * @apiVersion 0.1.0
     * @apiName 编辑资质分类
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id   资质主键，必传
     * @apiParam {string} lisence_name   资质名称，必传
     * @apiParam {file} icon    图标地址 ，必传
     * @apiParam {number} sort    排序，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93116  渠道新增失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93116,
     *          "message": "errorMsg"
     *      }
     */
    public function saveType(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id'   => 'required|numeric',
                'lisence_name' => 'required',
                'icon'         => 'required',
                'sort'         => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $lisence_name = $request->input('lisence_name');
        $param = $request->only(['lisence_id', 'icon', 'sort']);
        $icon = $request->file('icon');
        $res = ObsService::uploadFile($icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['icon'] = $res;
        }
        $param['lisence_name'] = $lisence_name;
        $detail = app('repo_lisence')->detail($param['lisence_id']);
        $param['level'] = $detail->level;
        $param['pause_icon'] = $detail->pause_icon;
        $param['exp_image'] = $detail->exp_image;
        $param['prompt_image'] = $detail->prompt_image;
        $param['prompt_voice'] = $detail->prompt_voice;
        app('repo_lisence')->saveType($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/del_type 删除资质分类
     * @apiVersion 0.1.0
     * @apiName 删除资质分类
     * @apiGroup 资质管理
     * @apiParam {string} ids   资质分类主键id（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93118  删除资质分类失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93118,
     *          "message": "errorMsg"
     *      }
     */
    public function delType(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $ids = $request->input('ids');
        $ids_arr = explode(',', $ids);
        app('repo_lisence')->delType($ids_arr);

        return Response::sendData(true);
    }


    /**
     * @api {post} /lisence/list 资质列表
     * @apiVersion 0.1.0
     * @apiName 资质列表
     * @apiGroup  资质管理
     * @apiParam {number} lisence_name    资质名称，非必传
     * @apiParam {number} status     1正常2暂停，非必传
     * @apiParam {number} page     页数(不传默认第一页)，非必传
     * @apiParam {number} perPage     每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list（数据列表）": [
     * {
     * "lisence_id（主键id）": 17,
     * "lisence_name（资质名称）": "王者荣耀",
     * "icon（图表地址）": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/test/lisence_icon/8a1acef9ed4c7b4ce54f3486b932f1c06A50W.png",
     * "square_icon（方形图标）": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/test/lisence_icon/8a1acef9ed4c7b4ce54f3486b932f1c06A50W.png",
     * "sort（排序）": 500,
     * "pid（父id）": 2,
     * "level（层级）": 2,
     * "add_time（添加时间）": "2020-08-28 15:54:00",
     * "status（状态：1正常 2暂停 3删除）": 1,
     * "pause_icon（暂停图标地址）": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/test/lisence_icon/06dc3ddcd5bf641dc75ef38c093a9baellrhX.png",
     * "exp_image（示例图片地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/8620edc62ad36acca1451b8880b46996.jpg",
     * "prompt_image（图片说明）": "这是图片说明",
     * "prompt_voice（语音说明）": "这是声音说明",
     * "price_arr（价格id数组）": "[10,20,50,70,100,120,150,200,250,300]",
     * "unit（单位）": "气泡币/局",
     * "select_arr（查询id数组）": "[10,20,50,70,100,120,150,200,250,300]",
     * "parent_name(父级名称)": "游戏"
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": "1",
     * "next": "http://admin.rh/lisence/list?=2",
     * "total": 15
     * },
     * "header(搜索条件)": {
     * "lisence_name（资质名称）": "英雄",
     * "status（资质状态：1正常 2暂停）": "1"
     * }
     * }
     * }
     */

    public function  list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_name' => 'filled',
                'status'       => 'filled|in:1,2',
                'page'         => 'filled|numeric',
                'perPage'      => 'filled|numeric',
            ],
            [
                'lisence_name.filled' => '资质名称不能为空',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $param = $request->only(['lisence_name', 'status']);
        $list = app('repo_lisence')->list($param, $page, $perPage);
        if ($list) {
            foreach ($list as $key => $value) {
                $value->add_time = date('Y-m-d H:i:s', $value->add_time);
                $detail = app('repo_lisence')->detail($value->pid);
                $value->parent_name = $detail->lisence_name;
            }
        }

        return Response::sendList($list, [], $param);
    }

    /**
     * @api {post} /lisence/add_lisence 新增资质
     * @apiVersion 0.1.0
     * @apiName 新增资质
     * @apiGroup 资质管理
     * @apiParam {string} lisence_name   资质名称，必传
     * @apiParam {file} icon    图标地址 ，必传
     * @apiParam {file} square_icon   方形图标 ，必传
     * @apiParam {number} sort    排序，非必传
     * @apiParam {file} pause_icon    暂停图标地址，必传
     * @apiParam {file} exp_image     示例图片地址，必传
     * @apiParam {string} prompt_image    图片说明，必传
     * @apiParam {string} prompt_voice    语音说明，必传
     * @apiParam {array} price_arr    价格数组id，必传
     * @apiParam {string} unit    单位，必传
     * @apiParam {array} select_arr    查询数组id，必传
     * @apiParam {number} pid    父id，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93119  新增资质失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93119,
     *          "message": "errorMsg"
     *      }
     */
    public function add_lisence(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_name' => 'required',
                'icon'         => 'required',
                'sort'         => 'filled|numeric',
                'pause_icon'   => 'required',
                'square_icon'  => 'required',
                'exp_image'    => 'filled',
                'prompt_image' => 'required',
                'prompt_voice' => 'required',
                'pid'          => 'required|numeric',
                'unit'         => 'required',
                'price_arr'    => 'required|array',
                'select_arr'   => 'required|array',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['lisence_name', 'sort', 'prompt_image', 'prompt_voice', 'price_arr', 'unit', 'select_arr', 'pid']);
        if (isset($param['price_arr'])) {
            if (count($param['price_arr']) != count(array_unique($param['price_arr']))) {
                throw new BasicException(50013);
            }
            foreach ($param['price_arr'] as $value) {
                if (!is_numeric($value)) {
                    throw new BasicException(50014);
                }
            }
            $param['price_arr'] = json_encode($param['price_arr']);
        }
        if (isset($param['select_arr'])) {
            if (count($param['select_arr']) != count(array_unique($param['select_arr']))) {
                throw new BasicException(50015);
            }
            foreach ($param['select_arr'] as $value) {
                if (!is_numeric($value)) {
                    throw new BasicException(50016);
                }
            }
            $param['select_arr'] = json_encode($param['select_arr']);
        }
        $square_icon = $request->file('square_icon');
        $res = ObsService::uploadFile($square_icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['square_icon'] = $res;
        }
        $icon = $request->file('icon');
        $res = ObsService::uploadFile($icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['icon'] = $res;
        }
        $pause_icon = $request->file('pause_icon');
        $res = ObsService::uploadFile($pause_icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['pause_icon'] = $res;
        }
        $param['exp_image'] = '';
        $exp_image = $request->file('exp_image');
        $res = ObsService::uploadFile($exp_image, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['exp_image'] = $res;
        }
        $param['add_time'] = time();
        $param['level'] = 2;
        app('repo_lisence')->add($param);

        return Response::sendData(true);
    }


    /**
     * @api {post} /lisence/save_lisence 编辑资质
     * @apiVersion 0.1.0
     * @apiName 编辑资质
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id   资质名称，必传
     * @apiParam {string} lisence_name   资质名称，必传
     * @apiParam {file} icon    图标地址 ，必传
     * @apiParam {file} square_icon    方形图标 ，必传
     * @apiParam {number} sort    排序，非必传
     * @apiParam {file} pause_icon    暂停图标地址，必传
     * @apiParam {file} exp_image     示例图片地址，必传
     * @apiParam {string} prompt_image    图片说明，必传
     * @apiParam {string} prompt_voice    语音说明，必传
     * @apiParam {array} price_arr    价格数组id，必传
     * @apiParam {string} unit    单位，必传
     * @apiParam {array} select_arr    查询数组id，必传
     * @apiParam {number} pid    父id，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93120  编辑资质失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93120,
     *          "message": "errorMsg"
     *      }
     */

    public function save_lisence(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id'   => 'required|numeric',
                'lisence_name' => 'required',
                'icon'         => 'required',
                'square_icon'  => 'required',
                'sort'         => 'filled|numeric',
                'pause_icon'   => 'required',
                'exp_image'    => 'filled',
                'prompt_image' => 'required',
                'prompt_voice' => 'required',
                'pid'          => 'required|numeric',
                'unit'         => 'required',
                'price_arr'    => 'required|array',
                'select_arr'   => 'required|array',
            ]
        );

        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['lisence_id', 'lisence_name', 'sort', 'prompt_image', 'prompt_voice', 'price_arr', 'unit', 'select_arr', 'pid', 'icon', 'pause_icon', 'exp_image', 'square_icon']);
        if (isset($param['price_arr'])) {
            if (count($param['price_arr']) != count(array_unique($param['price_arr']))) {
                throw new BasicException(50013);
            }
            foreach ($param['price_arr'] as $value) {
                if (!is_numeric($value)) {
                    throw new BasicException(50014);
                }
            }
            $param['price_arr'] = json_encode($param['price_arr']);
        }
        if (isset($param['select_arr'])) {
            if (count($param['select_arr']) != count(array_unique($param['select_arr']))) {
                throw new BasicException(50015);
            }
            foreach ($param['select_arr'] as $value) {
                if (!is_numeric($value)) {
                    throw new BasicException(50016);
                }
            }
            $param['select_arr'] = json_encode($param['select_arr']);
        }

        $square_icon = $request->file('square_icon');
        $res = ObsService::uploadFile($square_icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['square_icon'] = $res;
        }
        $icon = $request->file('icon');
        $res = ObsService::uploadFile($icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['icon'] = $res;
        }
        $pause_icon = $request->file('pause_icon');
        $res = ObsService::uploadFile($pause_icon, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['pause_icon'] = $res;
        }
        $exp_image = $request->file('exp_image');
        $res = ObsService::uploadFile($exp_image, ['image/png', 'image/jpeg', 'image/jpg']);
        if ($res) {
            $param['exp_image'] = $res;
        }
        app('repo_lisence')->save($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/operate_list 用于新增、编辑的资质分类列表
     * @apiVersion 0.1.0
     * @apiName 用于新增、编辑的资质分类列表
     * @apiGroup 资质管理
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "lisence_id(主键id)": 1,
     * "lisence_name（资质名称）": "娱乐",
     * "icon（图片）": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/test/lisence_icon/a45c6bd4bedef4c3a116e040ced43814hufxp.png"
     * }
     * ]
     * }
     */
    public function operate_list()
    {
        $list = app('repo_lisence')->operateList();

        return Response::sendData($list);
    }

    /**
     * @api {post} /lisence/del_lisence 删除资质
     * @apiVersion 0.1.0
     * @apiName 删除资质
     * @apiGroup 资质管理
     * @apiParam {string} ids   资质分类主键id（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93121  删除资质失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93121,
     *          "message": "errorMsg"
     *      }
     */
    public function del_lisence(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $ids = $request->input('ids');
        $ids_arr = explode(',', $ids);
        app('repo_lisence')->delLisence($ids_arr);
        app('repo_lisence_apply')->delLisenceApply($ids_arr);
        app('repo_user_lisence')->delUserLisence($ids_arr);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/suspend 暂停资质
     * @apiVersion 0.1.0
     * @apiName 暂停资质
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id  资质主键，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93122  暂停资质失败
     * @apiErrorExample 错误响应
     *      {
     *          "code": 93122,
     *          "message": "errorMsg"
     *      }
     */
    public function suspend(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $id = $request->input('lisence_id');
        DB::beginTransaction();
        try {
            app('repo_lisence')->suspend($id);
            $apply_data = app('repo_lisence_apply')->issetData($id);
            if ($apply_data) {
                app('repo_lisence_apply')->suspendApply($id);
            }
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(50017);
        }

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/open_lisence 开启资质
     * @apiVersion 0.1.0
     * @apiName 开启资质
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id  资质主键，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public function open_lisence(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $lisence_id = $request->input('lisence_id');
        app('repo_lisence')->open($lisence_id);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/examine_result   审核资质
     * @apiVersion 0.1.0
     * @apiName 审核资质
     * @apiGroup 资质管理
     * @apiParam {number} id  审核资质主键id，必传
     * @apiParam {string} verify_memo  审核通过或者失败原因，必传
     * @apiParam {number} type  审核类型：2 审核失败  3审核成功   资质审核类型，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public function examine_result(Request $request)
    {
        $token = $request->header('token', '');
        $admin_id = JwtService::getInfoFromToken($token, 'admin_id');
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id'          => 'required|numeric',
                'verify_memo' => 'required',
                'type'        => 'required|in:2,3',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }

        $id = $request->input('id');
        $type = $request->input('type');
        $verify_memo = $request->input('verify_memo');
        DB::beginTransaction();
        //审核失败执行代码
        if ($type == 2) {
            try {
                app('repo_lisence_apply')->examineFail($id, $admin_id, $verify_memo);
                $isHaveData = app('repo_user_lisence')->isHaveData($id);
                if ($isHaveData) {
                    app('repo_user_lisence')->examineFail($id);
                }
                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
                throw new  BasicException(93126);
            }
        }
        if ($type == 3) {
            $detail = app('repo_lisence_apply')->applyDetail($id);
            $lisence_detail = app('repo_lisence')->lisenceDetail($detail->lisence_id);
            if ($lisence_detail) {
                $unit = $lisence_detail->unit;
            } else {
                $unit = '';
            }
            if (!empty($lisence_detail->price_arr)) {
                $price_arr = json_decode($lisence_detail->price_arr, true);
                $price_key = array_search(min($price_arr), $price_arr);
                $price = $price_arr[$price_key];
            } else {
                $price = 0;
            }
            $user_lisence_data = [
                'status'      => 1,
                'is_open'     => 1,
                'update_time' => time(),
                'avatar'      => $detail->avatar,
                'lisence_pic' => $detail->lisence_pic,
                'voice'       => $detail->voice,
                'memo'        => $detail->memo,
                'price'       => $price,
                'max_price'   => $price,
                'total_num'   => 0,
                'score'       => 0,
                'unit'        => $unit,
                'tag_ids'     => $detail->tag_ids,
                'tag_image'   => $detail->tag_image,
                'duration'    => $detail->duration,
                'is_verify'   => 1,
            ];
            try {
                app('repo_lisence_apply')->examineSuccess($id, $admin_id, $verify_memo);
                $isHaveData = app('repo_user_lisence')->isHaveData($id);
                if ($isHaveData) {
                    app('repo_user_lisence')->examineSuccess($id, $user_lisence_data);
                }
                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
                throw new  BasicException(93126);
            }
        }

        return Response::sendData(true);
    }


    /**
     * @api {post} /lisence/lisence_apply_list 资质审核列表
     * @apiVersion 0.1.0
     * @apiName 资质审核列表
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id  资质主键，非必传
     * @apiParam {number} user_code  用户编号，非必传
     * @apiParam {number} status  状态：1待审核  2审核失败  3审核成功，非必传
     * @apiParam {number} page  页数（不传默认第一页），非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id(主键id)": 65,
     * "lisence_id（资质id）": 17,
     * "avatar（资质头像）": "https://img1.daofengdj.com/uploads/avatar/20200819/ukthcplfjsmapixarag9lidmbjpt77ha.jpeg",
     * "status（状态： 1待审核 2审核失败 3审核成功）": 3,
     * "lisence_pic（资质图片地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/a389c23300b6523a56b3c6d921a1cc58.jpg",
     * "voice（声音地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/7eda4526d5f1d242a2fda3eb4297b22d.amr",
     * "memo（接单说明）": "事业嗯widowwww找我噢耶",
     * "admin_id（管理员编号）": 0,
     * "verify_memo（审核意见）": "审核通过",
     * "add_time（提交时间）": "2020-09-12  09:57:50",
     * "update_time（审核时间）": "",
     * "user_id（用户id）": 642831,
     * "duration（录音时长）": 20,
     * "license_name（资质名称）": "王者荣耀",
     * "user_code（用户编号）": 922387,
     * "nickname（用户昵称）": "气泡用户922387",
     * "tag_name（我的段位）": "最强王者",
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 2
     * },
     * "header（搜索条件）": {
     * "lisence_id（资质id）": "17",
     * "user_id（用户id）": "642831",
     * "status(状态：1待审核 2审核失败 3审核成功)": "1"
     * }
     * }
     * }
     */
    public function lisence_apply_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id' => 'filled|numeric',
                'user_code'  => 'filled|numeric',
                'status'     => 'filled|in:1,2,3',
                'page'       => 'filled|numeric',
                'perPage'    => 'filled|numeric',
            ],
            [
                'user_code.filled'  => '下单人编号不能为空',
                'user_code.numeric' => '下单人编号必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $param = $request->only(['lisence_id', 'user_code', 'status']);
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $list = app('repo_lisence_apply')->applyList($param, $page, $perPage);
        if ($list) {
            foreach ($list as $key => $value) {
                $value->license_name = $value->lisence->lisence_name;
                $value->user_code = $value->user_data->user_code ?? '';
                $value->nickname = $value->user_data->nickname ?? '';
                $value->add_time = date('Y-m-d  H:i:s', $value->add_time);
                if (!empty($value->update_time)) {
                    $value->update_time = date('Y-m-d  H:i:s', $value->update_time);
                } else {
                    $value->update_time = '';
                }
                $uniqueTag = app('repo_user_lisence')->uniqueTag($value->id);
                if (!$uniqueTag) {
                    $value->tag_name = '';
                } else {
                    $name = app('repo_lisence_tag')->getName($uniqueTag);
                    $value->tag_name = $name;
                }
                unset($value->user_data);
                unset($value->lisence);
                unset($value->tag_ids);
                unset($value->tag_image);
                unset($value->is_show);
            }
        }

        return Response::sendList($list, [], $param);
    }

    /**
     * @api {post} /lisence/lisence_apply_detail 资质审核详情
     * @apiVersion 0.1.0
     * @apiName 资质审核详情
     * @apiGroup 资质管理
     * @apiParam {number} id  主键id，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "title(标题)": "游戏ID",
     * "content（位置内容）": "‘啦咯啦咯啦啦"
     * }
     * ]
     * }
     */
    public function lisence_apply_detail(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }

        $id = $request->input('id');
        $tag_arr = [];
        $apply_detail = app('repo_lisence_apply')->saveShowApplyData($id);
        if (isset($apply_detail->tag_ids) && !empty($apply_detail->tag_ids)) {
            $tags = explode(',', $apply_detail->tag_ids);
            $tag_arr = app('repo_lisence_tag')->tagList($tags);
        }
        $image_data = [];
        foreach (json_decode($apply_detail->tag_image, true) as $key => $val) {
            if (!isset($tag_arr[$key])) {
                continue;
            }
            if (is_array($val)) {
                $content = '';

                foreach ($val as $nal) {
                    if (!isset($tag_arr[$nal])) {
                        continue;
                    }

                    $content .= $tag_arr[$nal] . '  ';
                }
            } else {
                $content = isset($tag_arr[$val]) ? $tag_arr[$val] : $val;
            }
            $image_data[] = [
                'title'   => $tag_arr[$key],
                'content' => $content,
            ];
        }

        return Response::sendData($image_data);
    }


    /**
     * @api {post} /lisence/lisence_apply_show_list 资质审核不显示列表
     * @apiVersion 0.1.0
     * @apiName 资质审核不显示列表
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id  资质主键，非必传
     * @apiParam {number} user_code  用户编号，非必传
     * @apiParam {number} status  状态：1待审核  2审核失败  3审核成功，非必传
     * @apiParam {number} page  页数（不传默认第一页），非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id(主键id)": 65,
     * "lisence_id（资质id）": 17,
     * "avatar（资质头像）": "https://img1.daofengdj.com/uploads/avatar/20200819/ukthcplfjsmapixarag9lidmbjpt77ha.jpeg",
     * "status（状态： 1待审核 2审核失败 3审核成功）": 3,
     * "lisence_pic（资质图片地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/a389c23300b6523a56b3c6d921a1cc58.jpg",
     * "voice（声音地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/7eda4526d5f1d242a2fda3eb4297b22d.amr",
     * "memo（接单说明）": "事业嗯widowwww找我噢耶",
     * "admin_id（管理员编号）": 0,
     * "verify_memo（审核意见）": "审核通过",
     * "add_time（提交时间）": "2020-09-12  09:57:50",
     * "update_time（审核时间）": "",
     * "user_id（用户id）": 642831,
     * "duration（录音时长）": 20,
     * "license_name（资质名称）": "王者荣耀",
     * "user_code（用户编号）": 922387,
     * "nickname（用户昵称）": "气泡用户922387"
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 2
     * },
     * "header（搜索条件）": {
     * "lisence_id（资质id）": "17",
     * "user_id（用户id）": "642831",
     * "status(状态：1待审核 2审核失败 3审核成功)": "1"
     * }
     * }
     * }
     */
    public function lisence_apply_show_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id' => 'filled|numeric',
                'user_code'  => 'filled|numeric',
                'status'     => 'filled|in:1,2,3',
                'page'       => 'filled|numeric',
                'perPage'    => 'filled|numeric',
            ],
            [
                'lisence_id.filled'  => '资质编号不能为空',
                'lisence_id.numeric' => '资质编号必须为数字',
                'user_code.filled'   => '用户编号不能为空',
                'user_code.numeric'  => '用户编号必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $param = $request->only(['lisence_id', 'user_code', 'status']);
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $list = app('repo_lisence_apply')->applyShowList($param, $page, $perPage);
        if ($list) {
            foreach ($list as $key => $value) {
                $value->license_name = $value->lisence->lisence_name;
                $value->user_code = $value->user_data->user_code;
                $value->nickname = $value->user_data->nickname;
                $value->add_time = date('Y-m-d  H:i:s', $value->add_time);
                if (!empty($value->update_time)) {
                    $value->update_time = date('Y-m-d  H:i:s', $value->update_time);
                } else {
                    $value->update_time = '';
                }
                unset($value->user_data);
                unset($value->lisence);
                unset($value->tag_ids);
                unset($value->tag_image);
                unset($value->is_show);
            }
        }

        return Response::sendList($list, [], $param);
    }

    /**
     * @api {post} /lisence/save_lisence_apply_data  编辑不显示的资质审核数据
     * @apiVersion 0.1.0
     * @apiName 编辑不显示的资质审核数据
     * @apiGroup 资质管理
     * @apiParam {number} id  资质请求id，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "id(资质申请主键)": 15,
     * "tag_ids（标签ID）": null,
     * "tag_image（标签镜像）": "{}",
     * "unique_tag（必须的tag_id）": 0
     * }
     * }
     */
    public function save_lisence_apply_data(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $id = $request->input('id');
        $detail = app('repo_lisence_apply')->saveShowApplyData($id);
        $unique_tag = app('repo_user_lisence')->isHaveData($id);
        if ($unique_tag) {
            $detail->unique_tag = $unique_tag->unique_tag;
        } else {
            $detail->unique_tag = '';
        }
        if ($detail) {
            $detail = $detail->toArray();
        } else {
            $detail = [];
        }

        return Response::sendData($detail);
    }

    /**
     * @api {post} /lisence/agree_lisence_apply 通过不显示的资质审核
     * @apiVersion 0.1.0
     * @apiName 通过不显示的资质审核
     * @apiGroup 资质管理
     * @apiParam {number} id  资质请求id，必传
     * @apiSuccessExample 成功响应
     * {"code": 200,"data": true}
     */

    public function agree_lisence_apply(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $id = $request->input('id');
        app('repo_lisence_apply')->saveShow($id);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/save_lisence_apply 编辑不显示资质审核
     * @apiVersion 0.1.0
     * @apiName 编辑不显示资质审核
     * @apiGroup 资质管理
     * @apiParam {number} id  资质审核主键id，必传
     * @apiParam {number} tag_ids  标签ID，必传
     * @apiParam {number} tag_image  标签镜像，必传
     * @apiParam {number} unique_tag  必须的tag_id，非必传
     * @apiSuccessExample 成功响应
     * {"code": 200,"data": true}
     */
    public function save_lisence_apply(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id'         => 'required|numeric',
                'tag_ids'    => 'required',
                'tag_image'  => 'required',
                'unique_tag' => 'filled',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['id', 'tag_ids', 'tag_image']);
        $unique_tag = $request->input('unique_tag', '');
        app('repo_lisence_apply')->saveShowApply($param);
        if ($unique_tag) {
            app('repo_user_lisence')->saveUniqueTag($param['id'], $unique_tag);
        }

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/save_max_price 编辑最大单价
     * @apiVersion 0.1.0
     * @apiName 编辑最大单价
     * @apiGroup 资质管理
     * @apiParam {number} id  用户资质主键id，必传
     * @apiParam {number} price  价格，必传
     * @apiSuccessExample 成功响应
     * {"code": 200,"data": true}
     */
    public function save_max_price(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id'    => 'required|numeric',
                'price' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['id', 'price']);
        app('repo_user_lisence')->saveMaxPrice($param);

        return Response::sendData(true);
    }


    /**
     * @api {post} /lisence/save_is_verify 编辑技术认证
     * @apiVersion 0.1.0
     * @apiName 编辑技术认证
     * @apiGroup 资质管理
     * @apiParam {number} id  用户资质主键id，必传
     * @apiParam {number} is_verify  是否技术认证：1否 2是，必传
     * @apiSuccessExample 成功响应
     * {"code": 200,"data": true}
     */

    public function save_is_verify(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id'        => 'required|numeric',
                'is_verify' => 'required|in:1,2',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['id', 'is_verify']);
        app('repo_user_lisence')->saveVerify($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/lisence_apply_search_list 资质审核搜索列表
     * @apiVersion 0.1.0
     * @apiName 资质审核搜索列表
     * @apiGroup 资质管理
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "status（资质状态）": {
     * "1": "待审核",
     * "2": "审核失败",
     * "3": "审核成功"
     * },
     * "lisence_list（资质列表）": [
     * {
     * "lisence_id（资质id）": 1,
     * "lisence_name（资质名称）": "娱乐"
     * }
     * ]
     * }
     * }
     */

    public function lisence_apply_search_list()
    {
        //资质审核状态
        $status = [
            '1' => '待审核',
            '2' => '审核失败',
            '3' => '审核成功',
        ];
        //资质列表
        $lisence_list = app('repo_lisence')->lisenceSearchList();
        $data = [
            'status'       => $status,
            'lisence_list' => $lisence_list,
        ];

        return Response::sendData($data);
    }

    /**
     * @api {post} /lisence/lisence_user_list  用户资质列表
     * @apiVersion 0.1.0
     * @apiName 用户资质列表
     * @apiGroup 资质管理
     * @apiParam {number} user_code  用户编号，非必传
     * @apiParam {number} lisence_id  资质主键，非必传
     * @apiParam {number} status  状态：1使用中2申请中3审核失败，非必传
     * @apiParam {number} is_open  是否接单: 1是2关闭，非必传
     * @apiParam {number} is_verify  是否技术认证:  1不是 2是，非必传
     * @apiParam {string} add_start_time  申请成功开始时间，非必传
     * @apiParam {string} add_end_time  申请成功结束时间，非必传
     * @apiParam {string} save_start_time  最后更新开始时间，非必传
     * @apiParam {string} save_end_time  最后更新结束时间，非必传
     * @apiParam {number} page  页数（不传默认第一页），非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list(数据列表)": [
     * {
     * "id（主键id）": 25,
     * "user_id（用户id）": 642844,
     * "lisence_id（资质主键）": 17,
     * "status（状态：1使用中2申请中3审核失败4已删除）": 1,
     * "is_open（是否接单： 1是2关闭）": 1,
     * "add_time（第一次申请成功时间）":"2020-09-11 13:24:03",
     * "update_time（最后一次更新时间）":"2020-09-11 13:24:03",
     * "avatar（头像地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/94fcde9a726046eb2cfe258f4dcb1b3a.jpg",
     * "lisence_pic（资质图片）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/94fcde9a726046eb2cfe258f4dcb1b3a.jpg",
     * "voice（声音地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/0dbe453ed24e542668428976de5a0cc7.amr",
     * "memo（接单说明）": "你好",
     * "price（单价）": 100,
     * "total_num（接单量）": 0,
     * "score（评分）": 0,
     * "unit（单位）": "局",
     * "max_price（可接单的最大单价）": 100,
     * "duration（duration）": 3,
     * "unique_tag（必须的tag_id）": 0,
     * "is_verify（是否技术认证： 1不是 2是）": 1,
     * "lisence_name（资质名字）": "王者荣耀",
     * "user_code（用户编号）": 108627,
     * "nickname（用户昵称）": "暧昧的时代"
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 1
     * },
     * "header（搜索条件）": {
     * "lisence_id（资质主键）": "17",
     * "user_code（用户编号）": "108627",
     * "status（状态：1使用中 2申请中 3审核失败）": "1",
     * "is_open（是否接单： 1是 2关闭）": "1",
     * "is_verify（是否技术认证： 1不是 2是）": "1",
     * "add_start_time（提交开始时间）": "2020-09-02",
     * "add_end_time（提交结束时间）": "2020-09-12",
     * "save_start_time（更新开始时间）": "2020-09-12",
     * "save_end_time（更新结束时间）": "2020-09-12",
     * }
     * }
     * }
     */
    public function lisence_user_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id'      => 'filled|numeric',
                'user_code'       => 'filled|numeric',
                'status'          => 'filled|in:1,2,3',
                'is_open'         => 'filled|in:1,2',
                'is_verify'       => 'filled|in:1,2',
                'add_start_time'  => 'filled|date',
                'add_end_time'    => 'filled|date|after_or_equal:add_start_time',
                'save_start_time' => 'filled|date',
                'save_end_time'   => 'filled|date|after_or_equal:save_start_time',
                'page'            => 'filled|numeric',
                'perPage'         => 'filled|numeric',
            ],
            [
                'user_code.filled'  => '下单人编号不能为空',
                'user_code.numeric' => '下单人编号必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $param = $request->only(['lisence_id', 'user_code', 'status', 'is_open', 'is_verify']);
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $add_start_time = $request->input('add_start_time', '');
        $add_end_time = $request->input('add_end_time', '');
        $save_start_time = $request->input('save_start_time', '');
        $save_end_time = $request->input('save_end_time', '');
        $add_time = CommonService::transTime($add_start_time, $add_end_time);
        $save_time = CommonService::transTime($save_start_time, $save_end_time);
        $param['add_start_time'] = $add_time['start_at'];
        $param['add_end_time'] = $add_time['end_at'];
        $param['save_start_time'] = $save_time['start_at'];
        $param['save_end_time'] = $save_time['end_at'];
        $list = app('repo_user_lisence')->userLisenceList($param, $page, $perPage, 1);
        if (!empty($add_start_time)) {
            $param['add_start_time'] = $add_start_time;
        } else {
            unset($param['add_start_time']);
        }
        if (!empty($add_end_time)) {
            $param['add_end_time'] = $add_end_time;
        } else {
            unset($param['add_end_time']);
        }
        if (!empty($save_start_time)) {
            $param['save_start_time'] = $save_start_time;
        } else {
            unset($param['save_start_time']);
        }
        if (!empty($save_end_time)) {
            $param['save_end_time'] = $save_end_time;
        } else {
            unset($param['save_end_time']);
        }
        if ($list) {
            foreach ($list as $key => $value) {
                $value->lisence_name = $value->lisence->lisence_name;
                $value->user_code = $value->user_data->user_code ?? '';
                $value->nickname = $value->user_data->nickname ?? '';
                $value->add_time = date('Y-m-d H:i:s', $value->add_time);
                if (!empty($value->update_time)) {
                    $value->update_time = date('Y-m-d H:i:s', $value->update_time);
                } else {
                    $value->update_time = '';
                }
                unset($value->user_data);
                unset($value->lisence);
                unset($value->last_apply_id);
                unset($value->tag_ids);
                unset($value->tag_image);
            }
        }

        return Response::sendList($list, [], $param);
    }


    /**
     * @api {post} /lisence/all_user_lisence_list  所有用户资质列表
     * @apiVersion 0.1.0
     * @apiName 所有用户资质列表
     * @apiGroup 资质管理
     * @apiParam {number} user_code  用户编号，非必传
     * @apiParam {number} lisence_id  资质主键，非必传
     * @apiParam {number} status  状态：1使用中2申请中3审核失败，非必传
     * @apiParam {number} is_open  是否接单: 1是2关闭，非必传
     * @apiParam {number} is_verify  是否技术认证:  1不是 2是，非必传
     * @apiParam {string} add_start_time  申请成功开始时间，非必传
     * @apiParam {string} add_end_time  申请成功结束时间，非必传
     * @apiParam {string} save_start_time  最后更新开始时间，非必传
     * @apiParam {string} save_end_time  最后更新结束时间，非必传
     * @apiParam {number} page  页数（不传默认第一页），非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list(数据列表)": [
     * {
     * "id（主键id）": 25,
     * "user_id（用户id）": 642844,
     * "lisence_id（资质主键）": 17,
     * "status（状态：1使用中2申请中3审核失败4已删除）": 1,
     * "is_open（是否接单： 1是2关闭）": 1,
     * "add_time（第一次申请成功时间）":"2020-09-11 13:24:03",
     * "update_time（最后一次更新时间）":"2020-09-11 13:24:03",
     * "avatar（头像地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/94fcde9a726046eb2cfe258f4dcb1b3a.jpg",
     * "lisence_pic（资质图片）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/94fcde9a726046eb2cfe258f4dcb1b3a.jpg",
     * "voice（声音地址）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/img/0dbe453ed24e542668428976de5a0cc7.amr",
     * "memo（接单说明）": "你好",
     * "price（单价）": 100,
     * "total_num（接单量）": 0,
     * "score（评分）": 0,
     * "unit（单位）": "局",
     * "max_price（可接单的最大单价）": 100,
     * "duration（duration）": 3,
     * "unique_tag（必须的tag_id）": 0,
     * "is_verify（是否技术认证： 1不是 2是）": 1,
     * "lisence_name（资质名字）": "王者荣耀",
     * "user_code（用户编号）": 108627,
     * "nickname（用户昵称）": "暧昧的时代"
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 1
     * },
     * "header（搜索条件）": {
     * "lisence_id（资质主键）": "17",
     * "user_code（用户编号）": "108627",
     * "status（状态：1使用中 2申请中 3审核失败）": "1",
     * "is_open（是否接单： 1是 2关闭）": "1",
     * "is_verify（是否技术认证： 1不是 2是）": "1",
     * "add_start_time（提交开始时间）": "2020-09-02",
     * "add_end_time（提交结束时间）": "2020-09-12",
     * "save_start_time（更新开始时间）": "2020-09-12",
     * "save_end_time（更新结束时间）": "2020-09-12",
     * }
     * }
     * }
     */

    public function all_user_lisence_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id'      => 'filled|numeric',
                'user_code'       => 'filled|numeric',
                'status'          => 'filled|in:1,2,3',
                'is_open'         => 'filled|in:1,2',
                'is_verify'       => 'filled|in:1,2',
                'add_start_time'  => 'filled|date_format:"Y-m-d"',
                'add_end_time'    => 'filled|date_format:"Y-m-d"|after_or_equal:add_start_time',
                'save_start_time' => 'filled|date_format:"Y-m-d"',
                'save_end_time'   => 'filled|date_format:"Y-m-d"|after_or_equal:save_start_time',
                'page'            => 'filled|numeric',
                'perPage'         => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['lisence_id', 'user_code', 'status', 'is_open', 'is_verify']);
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $add_start_time = $request->input('add_start_time', '');
        $add_end_time = $request->input('add_end_time', '');
        $save_start_time = $request->input('save_start_time', '');
        $save_end_time = $request->input('save_end_time', '');
        $add_time = CommonService::transTime($add_start_time, $add_end_time);
        $save_time = CommonService::transTime($save_start_time, $save_end_time);
        $param['add_start_time'] = $add_time['start_at'];
        $param['add_end_time'] = $add_time['end_at'];
        $param['save_start_time'] = $save_time['start_at'];
        $param['save_end_time'] = $save_time['end_at'];
        $list = app('repo_user_lisence')->userLisenceList($param, $page, $perPage, 1);
        if (!empty($add_start_time)) {
            $param['add_start_time'] = $add_start_time;
        } else {
            unset($param['add_start_time']);
        }
        if (!empty($add_end_time)) {
            $param['add_end_time'] = $add_end_time;
        } else {
            unset($param['add_end_time']);
        }
        if (!empty($save_start_time)) {
            $param['save_start_time'] = $save_start_time;
        } else {
            unset($param['save_start_time']);
        }
        if (!empty($save_end_time)) {
            $param['save_end_time'] = $save_end_time;
        } else {
            unset($param['save_end_time']);
        }
        if ($list) {
            foreach ($list as $key => $value) {
                $value->lisence_name = $value->lisence->lisence_name;
                $value->user_code = $value->user_data->user_code;
                $value->nickname = $value->user_data->nickname;
                $value->add_time = date('Y-m-d H:i:s', $value->add_time);
                if (!empty($value->update_time)) {
                    $value->update_time = date('Y-m-d H:i:s', $value->update_time);
                } else {
                    $value->update_time = '';
                }
                unset($value->user_data);
                unset($value->lisence);
                unset($value->last_apply_id);
                unset($value->tag_ids);
                unset($value->tag_image);
            }
        }

        return Response::sendList($list, [], $param);
    }


    /**
     * @api {post} /lisence/lisence_user_search_list  用户资质搜索条件列表
     * @apiVersion 0.1.0
     * @apiName 用户资质搜索条件列表
     * @apiGroup 资质管理
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "status(资质状态)": {
     * "1": "使用中",
     * "2": "申请中",
     * "3": "审核失败"
     * },
     * "is_open（是否接单）": {
     * "1": "已接单",
     * "2": "已关闭"
     * },
     * "is_verify（是否技术认证）": {
     * "1": "否",
     * "2": "是"
     * },
     * "lisence_list（资质列表）": [
     * {
     * "lisence_id（资质id）": 1,
     * "lisence_name（资质名称）": "娱乐"
     * }
     * ]
     * }
     * }
     */
    public function lisence_user_search_list()
    {
        //用户资质状态
        $status = [
            '1' => '使用中',
            '2' => '申请中',
            '3' => '审核失败',
        ];
        //是否接单
        $is_open = [
            '1' => '已接单',
            '2' => '已关闭',
        ];
        //是否技术认证
        $is_verify = [
            '1' => '否',
            '2' => '是',
        ];
        //资质列表
        $lisence_list = app('repo_lisence')->lisenceSearchList();
        $data = [
            'status'       => $status,
            'is_open'      => $is_open,
            'is_verify'    => $is_verify,
            'lisence_list' => $lisence_list,
        ];

        return Response::sendData($data);
    }

    /**
     * @api {post} /lisence/lisence_order_list  资质订单列表
     * @apiVersion 0.1.0
     * @apiName 资质订单列表
     * @apiGroup 资质管理
     * @apiParam {string} order_no  订单编号，非必传
     * @apiParam {number} user_code   下单人编号，非必传
     * @apiParam {number} status  状态：1待接单 2已取消 3已拒绝 4服务中 5服务完成 6已结算 7申诉中 8申请退款中 9成功退款 10拒绝退款，非必传
     * @apiParam {number} is_auto  操作方式: 1人工 2机器，非必传
     * @apiParam {number} is_comment  是否可以评论:  1可以 2不可以，非必传
     * @apiParam {number} is_settlement  是否已结算:  1否 2是，非必传
     * @apiParam {string} add_start_time  下单开始时间，非必传
     * @apiParam {string} add_end_time  下单结束时间，非必传
     * @apiParam {string} save_start_time  最后更新开始时间，非必传
     * @apiParam {string} save_end_time  最后更新结束时间，非必传
     * @apiParam {number} page  页数（不传默认第一页），非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "order_id(订单id)": 16,
     * "order_no（订单号）": "20200909165434547190714",
     * "user_id（下单用户id）": 547190,
     * "uid（大神用户id）": 642768,
     * "add_time（下单时间）": "2020-09-09  16:54:34",
     * "status（订单状态）": 1,
     * "ulisence_id（用户资质id）": 21,
     * "lisence_id（资质id）": 17,
     * "order_num（订单数量）": 1,
     * "price（总价）": 0,
     * "memo（下单备注）": "我要找个大神带我玩",
     * "rate_platform（平台费率）": 20,
     * "rate_sociaty（工会费率）": 0,
     * "update_time（最后一次操作时间）": "2020-09-09  16:54:34",
     * "sociaty_id（工会id）": 0,
     * "response_type（相应类别）": 0,
     * "response_memo（相应详情）": null,
     * "unit（单位）": null,
     * "is_auto（操作方式：1人工  2机器）": 1,
     * "is_comment（是否可以评论：1可以 2不可以）": 1,
     * "is_settlement（是否已结算：1否 2是）": 1,
     * "demand_id（派单厅派单需求id）": 0,
     * "lisence_name（资质名称）": "王者荣耀",
     * "user_code（下单用户编号）": 999980,
     * "nickname（下单用户昵称）": "话说多了都是扯淡"
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 1
     * },
     * "header（搜索条件）": {
     * "order_no（订单号）": "2",
     * "user_code（下单用户编号）": "999980",
     * "status（订单状态）": "1",
     * "is_auto（操作方式：1人工  2机器）": "1",
     * "is_comment（是否可以评论：1可以 2不可以）": "1",
     * "is_settlement（是否已结算：1否 2是）": "1",
     * "add_start_time（下单开始时间）": "2020-09-09",
     * "add_end_time（下单结束时间）": "2020-09-10",
     * "save_start_time（更新开始时间）": "2020-09-09",
     * "save_end_time（更新结束时间）": "2020-09-10"
     * }
     * }
     * }
     */
    public function lisence_order_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'order_no'        => 'filled',
                'user_code'       => 'filled|numeric',
                'status'          => 'filled|in:1,2,3,4,5,6,7,8,9,10',
                'is_auto'         => 'filled|in:1,2',
                'is_comment'      => 'filled|in:1,2',
                'is_settlement'   => 'filled|in:1,2',
                'add_start_time'  => 'filled|date_format:"Y-m-d"',
                'add_end_time'    => 'filled|date_format:"Y-m-d"|after_or_equal:add_start_time',
                'save_start_time' => 'filled|date_format:"Y-m-d"',
                'save_end_time'   => 'filled|date_format:"Y-m-d"|after_or_equal:save_start_time',
                'page'            => 'filled|numeric',
                'perPage'         => 'filled|numeric',
            ],
            [
                'order_no.filled'   => '订单编号不能为空',
                'user_code.filled'  => '下单人编号不能为空',
                'user_code.numeric' => '下单人编号必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $param = $request->only(['order_no', 'user_code', 'status', 'is_auto', 'is_comment', 'is_settlement', 'add_start_time', 'add_end_time', 'save_start_time', 'save_end_time']);
        $add_start_time = $request->input('add_start_time', '');
        $add_end_time = $request->input('add_end_time', '');
        $save_start_time = $request->input('save_start_time', '');
        $save_end_time = $request->input('save_end_time', '');
        $add_time = CommonService::transTime($add_start_time, $add_end_time);
        $save_time = CommonService::transTime($save_start_time, $save_end_time);
        $list = app('repo_lisence_order')->orderList($add_time, $save_time, $param, $page, $perPage);
        if ($list) {
            foreach ($list as $key => $value) {
                $value->lisence_name = $value->lisence->lisence_name;
                $value->user_code = $value->user_data->user_code;
                $value->nickname = $value->user_data->nickname;
                $value->add_time = date('Y-m-d  H:i:s', $value->add_time);
                if (!empty($value->update_time)) {
                    $value->update_time = date('Y-m-d  H:i:s', $value->update_time);
                } else {
                    $value->update_time = '';
                }
                unset($value->lisence);
                unset($value->user_data);
            }
        }

        return Response::sendList($list, [], $param);
    }

    /**
     * @api {post} /lisence/order_log_list  资质订单日志列表
     * @apiVersion 0.1.0
     * @apiName 资质订单日志列表
     * @apiGroup 资质管理
     * @apiParam {string} order_id  订单主键id，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id(日志主键id)": 10,
     * "order_id（订单号）": 10,
     * "add_time（添加时间）": "2020-09-07 10:50:23",
     * "is_auto（操作类别：1人工 2机器）": 1,
     * "type（日志状态：1待接单 2已取消 3已拒绝 4服务中 5服务完成 6已结算 7申诉中 8申请退款中 9成功退款 10拒绝退款）": 1,
     * "memo（操作详细备注）": "大神闪电接单",
     * "memo_type（备注类别）": 0,
     * "admin_id(管理员id)": 0,
     * "admin_name(管理员名称)": "7777",
     * }
     * ]
     * }
     */
    public function order_log_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'order_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $order_id = $request->input('order_id');
        $list = app('repo_lisence_order_log')->orderLogList($order_id);
        if ($list) {
            foreach ($list as $key => &$value) {
                $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
                if (isset($value['admin']['admin_name'])) {
                    $value['admin_name'] = $value['admin']['admin_name'];
                } else {
                    $value['admin_name'] = '';
                }
                unset($value['admin']);
            }
        }

        return Response::sendData($list);
    }


    /**
     * @api {post} /lisence/order_comment_list  订单评论列表
     * @apiVersion 0.1.0
     * @apiName 订单评论列表
     * @apiGroup 资质管理
     * @apiParam {number} order_id  ，必传
     * @apiParam {string} order_no  订单号，非必传
     * @apiParam {number} user_code  用户编号，非必传
     * @apiParam {number} ulisence_id  用户资质id，非必传
     * @apiParam {number} page  页数（不传默认第一页），非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list(数据列表)": [
     * {
     * "comment_id（评论id）": 29,
     * "user_id（评论人用户id）": 642852,
     * "uid（被评论用户id）": 642799,
     * "ulisence_id（用户资质id）": 1,
     * "add_time（添加时间）": "2020-09-15 17:37:37",
     * "order_id（订单id）": 438,
     * "lisence_id（资质id）": 17,
     * "content（评论内容）": "引吭高歌他",
     * "score（评分）": 0,
     * "grade（等级：1好评 2中评 3差评）": 1,
     * "status（状态：1正常2待审核状态）": 1,
     * "mark（是否是第一次下单：1不是 2是）": 2,
     * "service_grade（服务满意度： 1满意 2一般 3不满意）": 3,
     * "special_grade（专业度满意度： 1满意 2一般 3不满意）": 3,
     * "experience_grade（体验满意度）": 3,
     * "order_no（订单号）": "20200915173511642852420",
     * "nickname（评论人用户昵称）": "一闪大神",
     * "user_code（评论人用户编号）": 842125
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 1
     * }
     * }
     * }
     */
    public function order_comment_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'order_no'    => 'filled',
                'user_code'   => 'filled|numeric',
                'ulisence_id' => 'filled|numeric',
                'order_id'    => 'filled|numeric',
            ],
            [
                'order_no.filled'     => '订单编号不能为空',
                'user_code.filled'    => '用户编号不能为空',
                'user_code.numeric'   => '用户编号必须为数字',
                'ulisence_id.filled'  => '用户资质id不能为空',
                'ulisence_id.numeric' => '用户资质id必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $param = $request->only(['order_no', 'user_code', 'ulisence_id', 'order_id']);
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);

        $list = app('repo_lisence_comment')->commentList($param, $page, $perPage);

        if ($list) {
            foreach ($list as $key => $value) {
                $value->add_time = date('Y-m-d H:i:s', $value->add_time);
                if (isset($value->lisence_order->order_no)) {
                    $value->order_no = $value->lisence_order->order_no;
                } else {
                    $value->order_no = '';
                }
                if (isset($value->user_data->nickname)) {
                    $value->nickname = $value->user_data->nickname;
                } else {
                    $value->nickname = '';
                }
                if (isset($value->user_data->user_code)) {
                    $value->user_code = $value->user_data->user_code;
                } else {
                    $value->user_code = '';
                }
                unset($value->lisence_order);
                unset($value->user_data);
                unset($value->pid);
                unset($value->level);
            }
        }

        return Response::sendList($list);
    }

    /**
     * @api {post} /lisence/one_order_comment_list  某一个订单评论列表
     * @apiVersion 0.1.0
     * @apiName 某一个订单评论列表
     * @apiGroup 资质管理
     * @apiParam {number} order_id  订单号，必传
     * @apiParam {number} page  页数（不传默认第一页），非必传
     * @apiParam {number} perPage  每页数量，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list(数据列表)": [
     * {
     * "comment_id（评论id）": 29,
     * "user_id（评论人用户id）": 642852,
     * "uid（被评论用户id）": 642799,
     * "ulisence_id（用户资质id）": 1,
     * "add_time（添加时间）": "2020-09-15 17:37:37",
     * "order_id（订单id）": 438,
     * "lisence_id（资质id）": 17,
     * "content（评论内容）": "引吭高歌他",
     * "score（评分）": 0,
     * "grade（等级：1好评 2中评 3差评）": 1,
     * "status（状态：1正常2待审核状态）": 1,
     * "mark（是否是第一次下单：1不是 2是）": 2,
     * "service_grade（服务满意度： 1满意 2一般 3不满意）": 3,
     * "special_grade（专业度满意度： 1满意 2一般 3不满意）": 3,
     * "experience_grade（体验满意度）": 3,
     * "order_no（订单号）": "20200915173511642852420",
     * "nickname（评论人用户昵称）": "一闪大神",
     * "user_code（评论人用户编号）": 842125
     * }
     * ],
     * "meta": {
     * "page": 1,
     * "limit": 20,
     * "next": null,
     * "total": 1
     * }
     * }
     * }
     */

    public function one_order_comment_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'order_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['order_id']);
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $list = app('repo_lisence_comment')->commentList($param, $page, $perPage);
        if ($list) {
            foreach ($list as $key => $value) {
                $value->add_time = date('Y-m-d H:i:s', $value->add_time);
                $value->order_no = $value->lisence_order->order_no;
                $value->nickname = $value->user_data->nickname;
                $value->user_code = $value->user_data->user_code;
                unset($value->lisence_order);
                unset($value->user_data);
                unset($value->pid);
                unset($value->level);
            }
        }

        return Response::sendList($list);
    }

    /**
     * @api {post} /lisence/order_refund  资质订单退款
     * @apiVersion 0.1.0
     * @apiName 资质订单退款
     * @apiGroup 资质管理
     * @apiParam {number} order_id  订单号，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public function order_refund(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'order_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $order_id = $request->input('order_id');
        $detail = app('repo_lisence_order')->orderDetail($order_id);
        if (!$detail) {
            throw new BasicException(50021);
        } else {
            if (!in_array($detail->status, [4, 5, 7, 8, 10])) {
                throw new BasicException(50022);
            }
        }
        $token = $request->header('token', '');
        $admin_id = JwtService::getInfoFromToken($token, 'admin_id');
        $orderLog = [
            'order_id'  => $order_id,
            'add_time'  => time(),
            'is_auto'   => 1,
            'type'      => 20,
            'memo_type' => '0',
            'memo'      => '管理员处理退款',
            'admin_id'  => $admin_id,
        ];
        $accountLog = [
            'user_id'     => $detail->user_id,
            'user_money'  => $detail->price,
            'change_time' => time(),
            'change_desc' => '退款处理超时系统自动退款，用户账户回款',
            'change_type' => 39,
            'order_sn'    => $detail->order_id,
        ];
        DB::beginTransaction();
        try {
            //修改订单状态
            app('repo_lisence_order')->saveStatus($order_id);
            //订单日志表添加数据
            app('repo_lisence_order_log')->addOrderLog($orderLog);
            //新增日志记录
            app('repo_account_log')->addLog($accountLog);
            //退款到用户账号
            app('repo_user')->refund($detail->price, $detail->user_id);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(50023);
        }

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/lisence_order_search_list  搜索资质订单列表
     * @apiVersion 0.1.0
     * @apiName 搜索资质订单列表
     * @apiGroup 资质管理
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "is_auto（订单操作方式）": {
     * "1": "人工",
     * "2": "机器"
     * },
     * "is_comment（订单是否可以评论）": {
     * "1": "是",
     * "2": "否"
     * },
     * "is_settlement（订单是否已结算）": {
     * "1": "否",
     * "2": "是"
     * },
     * "status（订单状态）": {
     * "1": "待接单",
     * "2": "已取消",
     * "3": "已拒绝",
     * "4": "服务中",
     * "5": "服务完成",
     * "6": "已结算",
     * "7": "申诉中",
     * "8": "申请退款中",
     * "9": "成功退款",
     * "10": "拒绝退款"
     * }
     * }
     * }
     */
    public function lisence_order_search_list(Request $request)
    {
        //订单操作方式
        $is_auto = [
            '1' => '人工',
            '2' => '机器',
        ];
        //订单是否可以评论
        $is_comment = [
            '1' => '是',
            '2' => '否',
        ];
        //订单是否已结算
        $is_settlement = [
            '1' => '否',
            '2' => '是',
        ];
        //订单状态
        $status = [
            '1'  => '待接单',
            '2'  => '已取消',
            '3'  => '已拒绝',
            '4'  => '服务中',
            '5'  => '服务完成',
            '6'  => '已结算',
            '7'  => '申诉中',
            '8'  => '申请退款中',
            '9'  => '成功退款',
            '10' => '拒绝退款',
        ];
        $data = [
            'is_auto'       => $is_auto,
            'is_comment'    => $is_comment,
            'is_settlement' => $is_settlement,
            'status'        => $status,
        ];

        return Response::sendData($data);
    }

    /**
     * @api {post} /lisence/lisence_tag_list  资质标签列表
     * @apiVersion 0.1.0
     * @apiName 资质标签列表
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id  资质id，必传
     * @apiParam {number} type  1分类标签  2 子标签，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "tag_id（标签主键id）": 2,
     * "lisence_id（资质id）": 17,
     * "tag_name（标签名）": "我的段位",
     * "pid（父id）": 0,
     * "level（级别）": 1,
     * "status（状态：1正常 2隐藏）": 1,
     * "sort（排序）": 9,
     * "option（标签模式（针对一级标签，1单选2多选3文本框））": 1,
     * "position（位置： 1普通 2段位置顶）": 1,
     * "is_select（是否是筛选条件1不是2是）": 2,
     * "add_time（添加时间）": "1970-01-01 08:00:00",
     * "description（提示性的文字）": "请选择你的段位",
     * "requirement（必要性：1必须2非必须）": 1,
     * "select_name（选项名字）": "段位",
     * "lisence_name（资质名称）": "王者荣耀"
     * }
     * ]
     * }
     */
    public function lisence_tag_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id' => 'required|numeric',
                'type'       => 'required|in:1,2',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['lisence_id', 'type']);
        $list = app('repo_lisence_tag')->tagTypeList($param);
        if ($list) {
            foreach ($list as $key => &$value) {
                $value['lisence_name'] = $value['lisence']['lisence_name'];
                $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
                unset($value['lisence']);
            }
        }

        return Response::sendData($list);
    }

    /**
     * @api {post} /lisence/add_type_tag 新增资质分类标签
     * @apiVersion 0.1.0
     * @apiName 新增资质分类标签
     * @apiGroup 资质管理
     * @apiParam {number} lisence_id  资质id，必传
     * @apiParam {string} tag_name  标签名称，必传
     * @apiParam {number} sort  排序，非必传
     * @apiParam {number} option  标签模式（针对一级标签，1单选2多选3文本框），必传
     * @apiParam {number} is_select  是否是筛选条件：1不是 2是，必传
     * @apiParam {number} description  描述，非必传
     * @apiParam {number} select_name 选项名字，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */

    public function add_type_tag(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id'  => 'required|numeric',
                'tag_name'    => 'required',
                'sort'        => 'filled|numeric',
                'option'      => 'required|in:1,2,3',
                'is_select'   => 'required|in:1,2',
                'description' => 'filled',
                'select_name' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['lisence_id', 'tag_name', 'sort', 'option', 'is_select', 'description', 'select_name']);
        $param['add_time'] = time();
        app('repo_lisence_tag')->addTypeTag($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/save_type_tag 编辑资质分类标签
     * @apiVersion 0.1.0
     * @apiName 编辑资质分类标签
     * @apiGroup 资质管理
     * @apiParam {number} tag_id  标签主键id，必传
     * @apiParam {number} lisence_id  资质id，必传
     * @apiParam {string} tag_name  标签名称，必传
     * @apiParam {number} sort  排序，非必传
     * @apiParam {number} option  标签模式（针对一级标签，1单选2多选3文本框），必传
     * @apiParam {number} is_select  是否是筛选条件：1不是 2是，必传
     * @apiParam {number} description  描述，非必传
     * @apiParam {number} select_name 选项名字，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */

    public function save_type_tag(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'tag_id'      => 'required|numeric',
                'lisence_id'  => 'required|numeric',
                'tag_name'    => 'required',
                'sort'        => 'filled|numeric',
                'option'      => 'required|in:1,2,3',
                'is_select'   => 'required|in:1,2',
                'description' => 'filled',
                'select_name' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['tag_id', 'lisence_id', 'tag_name', 'sort', 'option', 'is_select', 'description', 'select_name']);
        app('repo_lisence_tag')->saveTypeTag($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/del_tag 删除资质标签
     * @apiVersion 0.1.0
     * @apiName 删除资质标签
     * @apiGroup 资质管理
     * @apiParam {number} tag_ids  标签主键id（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public function del_tag(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'tag_ids' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $tag_ids = $request->input('tag_ids');
        $arr_tag_ids = explode(',', $tag_ids);
        app('repo_lisence_tag')->delTypeTag($arr_tag_ids);

        return Response::sendData(true);
    }

    /**
     * @api {post} /lisence/add_tag 新增资质标签
     * @apiVersion 0.1.0
     * @apiName 新增资质标签
     * @apiGroup 资质管理
     * @apiParam {string} tag_name  标签名称，必传
     * @apiParam {number} sort  排序，非必传
     * @apiParam {number} pid   父id，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public function add_tag(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'tag_name' => 'required',
                'sort'     => 'filled|numeric',
                'pid'      => 'required|numeric',
            ],
            [
                'sort.filled'  => '排序不能为空',
                'sort.numeric' => '排序必须为数字',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $param = $request->only(['tag_name', 'sort', 'pid']);
        $detail = app('repo_lisence_tag')->getDetail($param['pid']);
        $param['lisence_id'] = $detail->lisence_id;
        $param['add_time'] = time();
        $param['description'] = '';
        app('repo_lisence_tag')->addTypeTag($param);

        return Response::sendData(true);
    }


    /**
     * @api {post} /lisence/save_tag 编辑资质标签
     * @apiVersion 0.1.0
     * @apiName 编辑资质标签
     * @apiGroup 资质管理
     * @apiParam {string} tag_id  标签主键id，必传
     * @apiParam {string} tag_name  标签名称，必传
     * @apiParam {number} sort  排序，非必传
     * @apiParam {number} pid   父id，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     */
    public function save_tag(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'tag_id'   => 'required|numeric',
                'tag_name' => 'required',
                'sort'     => 'filled|numeric',
                'pid'      => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $param = $request->only(['tag_name', 'tag_id', 'sort', 'pid']);
        $detail = app('repo_lisence_tag')->getDetail($param['pid']);
        $param['lisence_id'] = $detail->lisence_id;
        app('repo_lisence_tag')->saveTypeTag($param);

        return Response::sendData(true);
    }


    /**
     * @api {post} /lisence/operate_tag_list 用于新增、编辑的标签分类列表
     * @apiVersion 0.1.0
     * @apiName 用于新增、编辑的标签分类列表
     * @apiGroup 资质管理
     * @apiParam {string} lisence_id  资质主键ID，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "option(标签模式)": {
     * "1": "单选",
     * "2": "多选",
     * "3": "文本框"
     * },
     * "is_select(是否是筛选条件)": {
     * "1": "不是",
     * "2": "是"
     * },
     * "tag_list(分类标签列表)": [
     * {
     * "tag_id（标签id）": 1,
     * "tag_name（标签名称）": "游戏ID"
     * }
     * ],
     * "lisence_list（资质列表）": [
     * {
     * "lisence_id（资质id）": 3,
     * "lisence_name（资质名称）": "歌手"
     * }
     * ]
     * }
     * }
     */
    public function operate_tag_list(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'lisence_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $lisence_id = $request->input('lisence_id');
        $list = app('repo_lisence_tag')->parentTagList($lisence_id);
        $option = [
            '1' => '单选',
            '2' => '多选',
            '3' => '文本框',
        ];
        $is_select = [
            '1' => '不是',
            '2' => '是',
        ];
        $lisence_list = app('repo_lisence')->lisenceSearchList();
        $data = [
            'option'       => $option,
            'is_select'    => $is_select,
            'tag_list'     => $list,
            'lisence_list' => $lisence_list,
        ];

        return Response::sendData($data);
    }


    //导出的一个例子
    //    public  function  export(){
    //        $list =app('repo_lisence')->lisenceSearchList();
    //        $headings = [
    //            '序号',
    //            '名称',
    //        ];
    //        $filename = '/admin_excel/'.date('YmdHis').mt_rand(100,999).'资质列表.xls';
    //        $res = app('excel')->store(new ExportService($list,$headings),$filename,'oss','Xls');
    //        if(!$res) throw new BasicException(90030);
    //        return 'http://'.env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').$filename;
    //    }

}
